package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.UniversityCostDto;
import xyz.zhouzhaodong.crm.dto.UniversityCostDto;
import xyz.zhouzhaodong.crm.dto.UniversityCostDto;
import xyz.zhouzhaodong.crm.entity.UniversityCost;
import xyz.zhouzhaodong.crm.entity.UniversityCost;
import xyz.zhouzhaodong.crm.entity.UniversityCost;
import xyz.zhouzhaodong.crm.mapper.UniversityCostMapper;
import xyz.zhouzhaodong.crm.mapper.UniversityCostMapper;
import xyz.zhouzhaodong.crm.mapper.UniversityMapper;
import xyz.zhouzhaodong.crm.service.IUniversityCostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 高校费用表 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-22
 */
@Service
public class UniversityCostServiceImpl extends ServiceImpl<UniversityCostMapper, UniversityCost> implements IUniversityCostService {

    @Resource
    UniversityCostMapper universityCostMapper;

    @Resource
    UniversityMapper universityMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addUniversityCost(UniversityCostDto universityCostDto) {
        try {
            UniversityCost universityCost = dtoChange(universityCostDto);
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            universityCost.setId(id);
            universityCost.setCreateUser(universityCostDto.getStaffId());
            universityCost.setCreateTime(new Date());
            universityCostMapper.insert(universityCost);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteUniversityCost(UniversityCostDto universityCostDto) {
        try {
            UniversityCost universityCost = new UniversityCost();
            universityCost.setId(universityCostDto.getId());
            universityCost.setUpdateUser(universityCostDto.getStaffId());
            universityCost.setUpdateTime(new Date());
            universityCost.setFlag(1);
            universityCostMapper.updateById(universityCost);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateUniversityCost(UniversityCostDto universityCostDto) {
        try {
            UniversityCost universityCost = dtoChange(universityCostDto);
            universityCost.setUpdateUser(universityCostDto.getStaffId());
            universityCost.setUpdateTime(new Date());
            universityCostMapper.updateById(universityCost);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectUniversityCost(UniversityCostDto universityCostDto) {
        Integer flag = universityCostDto.getFlag() == null ? 0 : universityCostDto.getFlag();
        if (universityCostDto.getPageNo() != 0 && universityCostDto.getPageSize() != 0){
            Page<UniversityCostDto> page = new Page<>(universityCostDto.getPageNo(), universityCostDto.getPageSize());
            universityCostDto.setFlag(flag);
            IPage<UniversityCostDto> list = universityCostMapper.selectListPage(page, universityCostDto);
            for(UniversityCostDto u : list.getRecords()){
                u.setUniversity(universityMapper.selectById(u.getUniversityId()));
            }
            return RestMessage.success(list);
        } else {
            QueryWrapper<UniversityCost> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("university_id", universityCostDto.getUniversityId());
            queryWrapper.eq("flag", flag);
            List<UniversityCost> list = universityCostMapper.selectList(queryWrapper);
            return RestMessage.success(list);
        }
    }

    private UniversityCost dtoChange(UniversityCostDto universityCostDto){
        UniversityCost universityCost = new UniversityCost();
        universityCost.setId(universityCostDto.getId());
        universityCost.setUniversityId(universityCostDto.getUniversityId());
        universityCost.setFeeJson(universityCostDto.getFeeJson());
        universityCost.setRemark(universityCostDto.getRemark());
        universityCost.setFlag(universityCostDto.getFlag());
        return universityCost;
    }
    
}
